package com.jsjosem.aftersales.ui.notifications;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.StateAdapter;
import com.jsjosem.aftersales.databinding.FragmentNotificationsBinding;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;

public class NotificationsFragment extends Fragment {

    private FragmentNotificationsBinding binding;
    private ConstraintLayout view;
    private List<String> stateList;
    private ViewPager2 viewPager2;
    private TabLayout tabLayout;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        view = binding.getRoot();
        initView();
        initTitle();
        return view;
    }

    private void initView() {
        viewPager2 = view.findViewById(R.id.notify_view_page);
        tabLayout = view.findViewById(R.id.tab_layout);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initTitle() {
        stateList = ListUtil.of("工单", "服务");
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(WorkTicketNotifyListFragment.newInstance());
        fragments.add(ServiceNotifyListFragment.newInstance());
        viewPager2.setAdapter(new StateAdapter((FragmentActivity) getContext(), fragments));
        new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            String state = stateList.get(position);
            tab.setText(state);
        }).attach();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
}